<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>{{ menuTitle.main }}</el-breadcrumb-item>
      <el-breadcrumb-item>{{ menuTitle.item }}</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card class="box-card">
      <el-row>
        <el-col>
          <el-button type="primary" @click="addDialogVisible = true">添加分类</el-button>
        </el-col>
      </el-row>

      <!-- 表格区 -->
      <el-table style="width: 100%" :data="cateList" border>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="cateName" label="分类名称"></el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="scope">{{ scope.row.createTime | dateFormat }}</template>
        </el-table-column>
        <el-table-column label="操作" width="226px">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="编辑分类信息" placement="top-start" :enterable="false">
              <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.id)">编辑分类</el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="删除分类信息" placement="top-start" :enterable="false">
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteById(scope.row.id)">删除分类</el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加分类对话框 -->
    <el-dialog title="添加分类" :visible.sync="addDialogVisible" width="50%" @close="addDialogClosed" :close-on-click-modal="false">
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px">
        <el-form-item label="分类名称" prop="cateName">
          <el-input v-model="addForm.cateName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑分类对话框 -->
    <el-dialog title="添加分类" :visible.sync="editDialogVisible" width="50%" @close="editDialogClosed" :close-on-click-modal="false">
      <el-form :model="editForm" :rules="addFormRules" ref="editFormRef" label-width="100px">
        <el-form-item label="分类名称" prop="cateName">
          <el-input v-model="editForm.cateName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 面包屑导航标题
      menuTitle: {
        main: '',
        item: ''
      },
      // 权限列表
      cateList: [],
      // 添加分类表单
      addForm: {
        cateName: ''
      },
      // 添加分类的校验规则
      addFormRules: {
        cateName: [
          { required: true, message: '请输入商品分类名称', trigger: 'blur' },
          { min: 2, max: 5, message: '分类名称长度 2-5 位', trigger: 'blur' }
        ]
      },
      // 控制添加分类对话框的显示与隐藏
      addDialogVisible: false,
      // 控制编辑分类对话框的显示与隐藏
      editDialogVisible: false,
      editForm: {}
    }
  },
  created() {
    this.getMenuList()
    this.getCateList()
  },
  methods: {
    // 获取面包屑导航标题
    async getMenuList() {
      const { data: res } = await this.$axios.get('/menu/getMenuList.do')
      if (!res.flag) return this.$message.error(res.message)
      // 获取当前激活路径
      const activePath = window.sessionStorage.getItem('activePath')
      // 循环判断激活路径对应的路径名称
      res.data.forEach(value => {
        value.menuItem.forEach(item => {
          if (item.path === activePath) {
            this.menuTitle.item = item.title
            this.menuTitle.main = value.title
          }
        })
      })
    },
    // 获取分类列表
    async getCateList() {
      const { data: res } = await this.$axios.get('/category/findAll.do')
      if (!res.flag) return this.$message.error(res.message)
      this.cateList = res.data
    },
    // 添加分类对话框关闭
    addDialogClosed() {
      // 重置表单数据
      this.$refs.addFormRef.resetFields()
    },
    // 添加分类
    addCate() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return false

        const { data: res } = await this.$axios.post('/category/add.do', this.addForm)
        if (!res.flag) return this.$message.error(res.message)

        this.$message.success(res.message)
        this.addDialogVisible = false
      })
    },
    // 展示编辑分类信息对话框
    async showEditDialog(id) {
      const { data: res } = await this.$axios.get(`/category/getCateById.do?id=${id}`)
      if (!res.flag) return this.$message.error(res.message)
      this.editForm = res.data
      this.editDialogVisible = true
    },
    // 编辑信息分类对话框关闭
    editDialogClosed() {
      this.$refs.editFormRef.resetFields()
    },
    // 编辑分类
    editCate() {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return false

        const { data: res } = await this.$axios.put('/category/edit.do', this.editForm)
        if (!res.flag) return this.$message.error(res.message)

        this.$message.success(res.message)
        this.editDialogVisible = false
        this.getCateList()
      })
    },
    async deleteById(id) {
      // 防止用户误操作
      const result = await this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(e => e)

      if (result !== 'confirm') return this.$message.info('已经取消删除')

      // 向后台发起请求, 删除指定 id 的用户
      const { data: res } = await this.$axios.delete(`/category/delete.do?id=${id}`)
      if (!res.flag) return this.$message.error(res.message)

      this.$message.success(res.message)
      this.getCateList()
    }
  }
}
</script>
